<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;

use AuctionBack\Model\ActivityExpertModel;
use AuctionBack\Model\ActivityModel;
use AuctionCore\Account\Back\Employee;

class ActivityLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    protected $activityModel;

    protected $activityExpertModel;

    public function _initialize()
    {
        $this->activityModel = new ActivityModel();
        $this->activityExpertModel = new ActivityExpertModel();
    }

    public function getActivityList($page, $pageSize, $params = [])
    {
        $page = $page < 1 ? 1 : (int)$page;
        $pageSize = $pageSize < 1 ? 10 : (int)$pageSize;
        $map = [];
        $map['status'] = ['gt', 0];
        if ($params['id']) {
            $map['activity_id'] = ['eq', $params['id']];
        }
        if ($params['name']) {
            $map['activity_name'] = ['LIKE', '%' . $params['name'] . '%'];
        }
        if ($params['emp_user']) {
            $empIds = Employee::getEmpIdByName($params['emp_user']);
            $map['emp_id'] = ['in', $empIds];
        }
        if ($params['start']) {
            $map['create_time'] = ['egt', strtotime($params['start'])];
        }
        if ($params['end']) {
            $map['create_time'] = ['elt', strtotime($params['end'])];
        }
        if ($params['start'] && $params['end']) {
            $map['create_time'] = [
                ['egt', strtotime($params['start'])],
                ['elt', strtotime($params['end'])]
            ];
        }
        if ($params['status'] > 0) {
            $map['status'] = ['eq', $params['status']];
        }
        $order = 'create_time DESC';
        $cols = 'activity_id,activity_name,emp_id,status,create_time';
        $total = $this->activityModel->where($map)->count();
        $list = $this->activityModel->field($cols)
            ->where($map)
            ->order($order)
            ->page($page, $pageSize)
            ->select();
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $employee = Employee::user_info_id($v['emp_id']);
                $list[$k]['emp_user_name'] = $employee['real_name'];
                $questionLogic = new QuestionLogic();
                $questionNum = $questionLogic->getActivityQuestionNum($v['activity_id']);
                $list[$k]['question_num'] = $questionNum;
                $list[$k]['status_name'] = $v['status'] > 0 && $v['status'] == 1 ? '已上线' : '已下线';
                $list[$k]['create_time'] = date("Y-m-d H:i:s", $v['create_time']);
            }
        }
        return [
            'total' => $total,
            'list'  => $list
        ];
    }

    public function getActivityById($activityId = 0)
    {
        if ($activityId < 1) {
            return false;
        }
        $condition = [];
        $condition['activity_id'] = $activityId;
        return $this->activityModel->where($condition)->find();
    }

    public function addActivity($data = [])
    {
        if (empty($data)) {
            return false;
        }
        $activityExperts = $data['activity_experts'];
        unset($data['activity_experts']);
        $data['status'] = 2;
        $data['update_time'] = NOW_TIME;
        $data['create_time'] = NOW_TIME;
        $activityId = $this->activityModel->add($data);
        foreach ($activityExperts as $k => $v) {
            $this->activityExpertModel->add(
                [
                    'activity_id'  => $activityId,
                    'expert_id'    => $k,
                    'expert_intro' => $v,
                    'update_time'  => NOW_TIME,
                    'create_time'  => NOW_TIME
                ]
            );
        }
        return $activityId;
    }

    public function updateActivity($data = [])
    {
        if (empty($data) || empty($data['activity_id'])) {
            return false;
        }
        $activityExperts = $data['activity_experts'];
        if (!empty($activityExperts)) {
            foreach ($activityExperts as $k => $v) {
                $condition['activity_id'] = $data['activity_id'];
                $condition['expert_id'] = $k;
                $expert = $this->activityExpertModel->where($condition)->find();
                if (!empty($expert)) {
                    $this->activityExpertModel->save(
                        [
                            'id'           => $expert['id'],
                            'expert_intro' => $v,
                            'update_time'  => NOW_TIME
                        ]
                    );
                    $map['activity_id'] = $data['activity_id'];
                    $map['expert_id'] = ['NEQ', $k];
                    $this->activityExpertModel->where($map)->delete();
                } else {
                    $this->activityExpertModel->add(
                        [
                            'activity_id'  => $data['activity_id'],
                            'expert_id'    => $k,
                            'expert_intro' => $v,
                            'update_time'  => NOW_TIME,
                            'create_time'  => NOW_TIME
                        ]
                    );
                }
            }
            unset($data['activity_experts']);
        }

        $data['update_time'] = NOW_TIME;
        return $this->activityModel->save($data);
    }

    /**
     * 获取活动对应的专家
     *
     * @param int $activityId
     */
    public function getActivityExpert($activityId = 0)
    {
        if (empty($activityId)) {
            return false;
        }
        return $this->activityExpertModel->where(['activity_id' => $activityId])->select();

    }
}